//
// Created by Huiyi on 2021/4/25.
//
#include "module_XExpand.h"

//unicode转ascii
CXTextA X_Coding_WToA (const wchar_t* wp, size_t m_encode)
{
    int len = WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), NULL, 0, NULL, NULL);
    char	*l_char = (char*)malloc(len + 1);
    WideCharToMultiByte(m_encode, 0, wp, wcslen(wp), l_char, len, NULL, NULL);
    l_char [len] = '\0';
    CXTextA rets = l_char;
    free(l_char);
    return  rets;
}

//ascii转unicode
CXText X_Coding_AToW(const char* c, size_t m_encode)
{
    int len = MultiByteToWideChar(m_encode, 0, c, strlen(c), NULL, 0);
    wchar_t*	l_wchar = new wchar_t [len + 1];
    MultiByteToWideChar(m_encode, 0, c, strlen(c), l_wchar, len);
    l_wchar [len] = '\0';
    CXText rets = l_wchar;
    delete [] l_wchar;
    return rets;
}

//Utf8转Gbk
CXTextA X_Coding_Utf82Gbk(const char* utf8)
{
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, 0);
    wchar_t* wstr = new wchar_t [len+1];
    memset(wstr, 0, len+1);
    MultiByteToWideChar(CP_UTF8, 0, utf8, -1, wstr, len);
    len = WideCharToMultiByte(CP_ACP, 0, wstr, -1, NULL, 0, NULL, NULL);
    char* str = (char*)malloc(len+1);
    memset(str, 0, len+1);
    WideCharToMultiByte(CP_ACP, 0, wstr, -1, str, len, NULL, NULL);
    if(wstr) { delete [] wstr; }
    CXTextA rets = str;
    free (str);
    return rets;
}

//Unicode转Utf8
CXTextA X_Coding_UnicodeToUtf8(const wchar_t* unicode)
{
    int len;
    len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);
    char *szUtf8 = (char*)malloc(len + 1);
    memset(szUtf8, 0, len + 1);
    WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);
    CXTextA rets = szUtf8;
    free(szUtf8);
    return rets;
}

//GBK转Utf8
CXTextA X_Coding_GBKToUtf8(const char* str)
{
    return X_Coding_UnicodeToUtf8(X_Coding_AToW(str).get());
}

//da_lao_han_pi
